package cc.vv.btpartyjob.partymanager.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.inter.OperationItemClickInterface
import cc.vv.btpartyjob.partymanager.moudel.bean.OperationBean
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lkimagecomponent2.LKImage
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode
import java.util.*

/**
 * 展示数据的自定义view
 * Created by  Chen.qi on 2018/10/24.
 */
class OperationView : LinearLayout {
    /**
     * 上下文
     */
    private var mContext: Context? = null
    /**
     * 父布局
     */
    private var llSelectedAll: LinearLayout? = null

    constructor(context: Context) : this(context, null) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        val inflateView = View.inflate(context, R.layout.layout_operation_view, null)
        llSelectedAll = inflateView.findViewById(R.id.ll_lov_showAll)
        this.addView(inflateView)
    }


    /**
     * 填充选项数据
     *
     * @param optionList 数据集
     * @param itemSize   每行有几个选项
     */
    fun addAllSelectedFileView(optionList: ArrayList<OperationBean>?, itemSize: Int,
                               itemClickCallBack: OperationItemClickInterface?) {
        if (null == optionList) {
            return
        }
        //清除
        clearAllJobOptionView()
        val length = optionList.size
        val sum = length / itemSize
        val add = if (length % itemSize == 0) 0 else 1
        val totalLinLayout = sum + add

        val viewWight = (LKScreenUtil.getScreenWidth() - LKScreenUtil.dp2px(46f)) / itemSize
        val layoutParams1 = LinearLayout.LayoutParams(viewWight, LayoutParams.WRAP_CONTENT)

        for (j in 0 until totalLinLayout) {
            val honLinearLayout = LinearLayout(context)
            honLinearLayout.setPadding(0, LKScreenUtil.dp2px(10f), 0, 0)
            val iNumber = j * itemSize
            var count = 0
            for (i in iNumber until optionList.size) {
                count++
                if (count == itemSize + 1) {
                    break
                }
                val view = LayoutInflater.from(mContext).inflate(R.layout.layout_job_option_item, null)
                view.layoutParams = layoutParams1
                val img = view.findViewById<ImageView>(R.id.iv_job_optionImg)
                val descText = view.findViewById<TextView>(R.id.tv_job_optionDesc)

                val layoutParams = img.layoutParams as LinearLayout.LayoutParams
                layoutParams.width = LKScreenUtil.dp2px(70f)
                layoutParams.height = LKScreenUtil.dp2px(70f)
                img.layoutParams = layoutParams

                val jobOptionObj = optionList[i]

                setPic(jobOptionObj.title, img)
                view.setOnClickListener {
                    itemClickCallBack?.clickItem(jobOptionObj)
                }
                descText.text = jobOptionObj.title

                val tv = TextView(mContext)
                val layoutParamstv = LinearLayout.LayoutParams(LKScreenUtil.dp2px(3f), ViewGroup.LayoutParams.WRAP_CONTENT)
                tv.layoutParams = layoutParamstv
                honLinearLayout.addView(tv)

                honLinearLayout.addView(view)
            }
            llSelectedAll?.addView(honLinearLayout)
        }
    }

    /**
     * 清除view
     */

    private fun clearAllJobOptionView() {
        llSelectedAll?.removeAllViews()
    }

    private fun setPic(title: String?, img: ImageView) {
        when (title) {
            "社区组织" -> {
                setP("icon_ptman_sqzz.png", img)
            }
            "网格" -> {
                setP("icon_ptman_grid.png", img)
            }
            "楼栋党小组" -> {
                setP("icon_ptman_lddxz.png", img)
            }
            "单元中心户" -> {
                setP("icon_ptman_dyzxh.png", img)
            }
            "社区活动" -> {
                setP("icon_ptman_sqhd.png", img)
            }
            "组织查询" -> {
                setP("icon_ptman_zzcx.png", img)
            }
            " 组织查询 " -> {
                setP("icon_ptman_zzcx2.png", img)
            }
            "单位报到" -> {
                setP("icon_ptman_dwbd.png", img)
            }
            "党员报到" -> {
                setP("icon_ptman_dybd.png", img)
            }
            "报到统计" -> {
                setP("icon_ptman_bdtj.png", img)
            }
            "党员承诺" -> {
                setP("icon_ptman_dycn.png", img)
            }
            "单位承诺" -> {
                setP("icon_ptman_dwcn.png", img)
            }
            "简介" -> {
                setP("icon_ptman_jianjie.png", img)
            }
            "活动" -> {
                setP("icon_ptman_hd.png", img)
            }
            "项目查询" -> {
                setP("icon_ptman_xmcx.png", img)
            }
            "添加项目" -> {
                setP("icon_ptman_tjxm.png", img)
            }
            "项目预警" -> {
                setP("icon_ptman_xmyj.png", img)
            }
            "项目统计" -> {
                setP("icon_ptman_xmtj.png", img)
            }
            "单位简介" -> {
                setP("icon_ptman_dwjj.png", img)
            }
            "评星晋级" -> {
                setP("icon_ptman_pxjj.png", img)
            }
            "政策文件" -> {
                setP("icon_ptman_zcwj.png", img)
            }
            "红领党建" -> {
                setP("icon_ptman_hldj.png", img)
            }
            "行政村管理" -> {
                setP("icon_ptman_xzcgl.png", img)
            }
            "四支队伍" -> {
                setP("icon_ptman_szdw.png", img)
            }
            "组织数据" -> {
                setP("icon_ptman_zzsj.png", img)
            }
            "人员统计" -> {
                setP("icon_ptman_rytj.png", img)
            }
            "在线统计" -> {
                setP("icon_ptman_zxtj.png", img)
            }
            "标准化数据" -> {
                setP("icon_ptman_bzhsj.png", img)
            }
        }
    }

    private fun setP(picStr: String?, img: ImageView) {
        LKImage.load().scale(ScaleMode.CENTER_CROP).loadAsserts(picStr).into(img)
    }

}